package com.gy.room;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Timer;  
import java.util.TimerTask;
import java.util.Vector;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;



import com.google.common.collect.Maps;

import messageControl.userStatesEnum;




public class roomManager {
	private static final roomManager instance = new roomManager();
    public static roomManager getInstance() {
        return instance;
    }
    //鎴块棿閿�
    private final ReadWriteLock roomLock = new ReentrantReadWriteLock();
    //Integer为int，AbstractRoom为房间
   private Map<Integer,AbstractRoom> roomMap = Maps.newConcurrentMap();

   
   private List<clientPlayInfo> waitringlist = new ArrayList<clientPlayInfo>();
	



	public void  Init() {
		// 获取随机生成的房间Id，设置类型
		createRooms();
		createTimer();
	}
	
	public void createTimer()
	{
		 Timer timer = new Timer();
		        
		     
		      //前一次程序执行开始 后 2000ms后开始执行下一次程序
		        timer.scheduleAtFixedRate(new TimerTask() {
		           @Override
		            public void run() {
		        	   update();
		            
		           }
		     },0,1000);
	}

	//玩家掉线超过次数则直接判为胜利
	public void forceOutRoom(clientPlayInfo player)
	{
		
	}
	
	public void outWaitringlist(clientPlayInfo player)
	{
		if(waitringlist.contains(player))
		{
			waitringlist.remove(player);
		}
		player.states=userStatesEnum.leavingLine;
	}
   private void roomUpdate()
   {

	   for (Map.Entry<Integer, AbstractRoom> entry : roomMap.entrySet()) { 
	   //  System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); 
	     entry.getValue().update();
	   }
   }
	
	//讲所有的等待玩家都放到房间中
   public void update()
   {
	  // System.out.println("IMP 当前时间" );
	   
	   
	   //便利所有的房间调用UPDATE
	   if(waitringlist.size()>0)
	   {
			Iterator<clientPlayInfo> it = waitringlist.iterator();
			while(it.hasNext()){
				clientPlayInfo str = it.next();
				getBestRoom().enterRoom(str);
				
				System.out.println(str.getName());		
				it.remove();			
			 }
	   }
 
	   if(roomMap.size()>0)
	   {
		   roomUpdate();
	   }
	  
 
   }
   
   //从头开始遍历，找出前面人数不满的房间
   public AbstractRoom getBestRoom()
   {

	   for (Map.Entry<Integer, AbstractRoom> entry : roomMap.entrySet()) { 
	     System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); 
	     if(!entry.getValue().isFull())
	     {
	    	 return entry.getValue();
	     }
	   }
	   return null;
   }
   public void enterWaitingRoom(clientPlayInfo usr)
   {
	   waitringlist.add(usr);
	   usr.setStates(userStatesEnum.matchMode);
   }

   
  
   
   /*创建指定的足够的房间*/

  public void createRooms()
  {
	  for(int i=0;i<100;i++)
	  {
		  AbstractRoom room = new AbstractRoom();
		  room.setId(randomRoomId());
		  addRoom(room);
	  }
  }
   
   public void createRoom()
   {
	   AbstractRoom room = new AbstractRoom();
		  room.setId(randomRoomId());
		  addRoom(room);
   }

	public Map<Integer, AbstractRoom> getRoomMap() {
		return roomMap;
	}



	public void setRoomMap(Map<Integer, AbstractRoom> roomMap) {
		this.roomMap = roomMap;
	}



	public AbstractRoom getRoomById(int roomId) {
    	return roomMap.get(roomId); //通过Key值返回玩家指定的房间，获取房间的ID返回
    }
    
   
    
    public void addRoom(AbstractRoom room) {
    	roomMap.put(room.getId(), room);//加入一个房间
    }
    
    public void removeRoom(AbstractRoom room) {
    	roomMap.remove(room.getId());//把房间从Map中删除
    }
    
    public int randomRoomId() {//生成随机ID，且不会和其他ID重复
    	int roomId = new Random().nextInt(100000)+100000;
    	while(roomMap.containsKey(roomId)) {
    		roomId = new Random().nextInt(100000)+100000;
    	}
    	return roomId;
    }



	
}
